//
//  YQHomeDetailController.m
//  EPA--1
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQHomeDetailController.h"
#import "YQHomeListModel.h"
#import "YQHomeListCell.h"
#import "YQNetWorkRequestManager.h"
#import "LORefresh.h"
#import "SDWebImageManager.h"
#import "YQReadViewController.h"
#import "YQCollectTableViewController.h"
@interface YQHomeDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *listModelArray;
/**
 *  请求的起始条数
 */
@property (nonatomic, strong) NSString *start;
@property (nonatomic, strong) LORefresh *headerRefresh;

@end

@implementation YQHomeDetailController

- (void)dealloc {
    [self.tableView removeObserver:[self.tableView gifHeader] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.listModelArray = [NSMutableArray new];
    self.start = @"0";
    [self loadDataFromServeType:self.type start:self.start];
    [self.tableView registerNib:[UINib nibWithNibName:@"YQHomeListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.separatorStyle = 0;
    __weak typeof (self)weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
        [weakSelf.listModelArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadDataFromServeType:weakSelf.type start:@"0"];
    }];
    self.headerRefresh = [self.tableView gifHeader];
    [self.headerRefresh setGifName:@"demo"];
//上提加载更多
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.listModelArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        [weakSelf loadDataFromServeType:weakSelf.type start:weakSelf.start];
    }];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"收藏列表" style:UIBarButtonItemStylePlain target:self action:@selector(collectAction:)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)collectAction:(UIBarButtonItem *)item
{
    
    YQCollectTableViewController *collect = [YQCollectTableViewController new];
    [self.navigationController pushViewController:collect animated:YES];
}
//
- (void)loadDataFromServeType:(NSString *)type start:(NSString *)start
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:READDETAILLIST_URL] method:YQ_POST params:@{@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"typeid":type,@"client":@"1",@"sort":@"addtime",@"limit":@10,@"version":@"3.0.2",@"auth":@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0",@"start":start} result:^(NSData *data, NSURLResponse *response, NSError *error) {
//         NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = dict[@"data"];
//             NSLog(@"%@",dataDict);
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                YQHomeListModel *model = [YQHomeListModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.listModelArray addObject:model];
            }
//             NSLog(@"1111%@",self.listModelArray);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [[self.tableView gifHeader] endRefreshing];
                [[self.tableView defaultFooter] endRefreshing];
            });

        }
            }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.listModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.listModelArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    YQHomeListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    return 170;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YQReadViewController *readVC = [YQReadViewController new];
    YQHomeListModel *model = self.listModelArray[indexPath.row];
    readVC.contentid = model.ID;
    readVC.model = model;
    [self.navigationController pushViewController:readVC animated:YES];
    
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
